import { defineStore, acceptHMRUpdate } from "pinia";
import { getCameras, getPlaybackUrl } from "../api";

export const useJinnongStore = defineStore("jinnong", {
  state: () => ({
    token: null,
    cameras: [],
    playbackUrl: null,
  }),

  getters: {},

  actions: {
    async loadCameras(workId) {
      const result = await getCameras(workId);
      this.cameras = result.data;
    },
    async getPlaybackUrl(data) {
      let result = await getPlaybackUrl(data);
      if (result.url) this.playbackUrl = result.url;
      else {
        data.recordLocation = 0;
        result = await getPlaybackUrl(data);
        if (result.url) this.playbackUrl = result.url;
      }
    },
  },
});

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useJinnongStore, import.meta.hot));
}
